package com.example.yls.demoa;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
      private Button bofan;
      private Button zanting;
    private MediaPlayer mediaPlayer;
    private SeekBar seekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       bofan = findViewById(R.id.bofan);
       zanting = findViewById(R.id.zanting);
       seekBar = findViewById(R.id.seekBar);

       new Thread(new Runnable() {
           @Override
           public void run() {
               while (true){
                   if (mediaPlayer != null && mediaPlayer.isPlaying()){
                       int position = mediaPlayer.getCurrentPosition();
                       int total = mediaPlayer.getDuration();
                       int progress =  100 * position/total;
                       seekBar.setProgress(progress);
                   }
                   try {
                       Thread.sleep(500);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           }
       }).start();

       seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
           @Override
           public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

           }

           @Override
           public void onStartTrackingTouch(SeekBar seekBar) {

           }

           @Override
           public void onStopTrackingTouch(SeekBar seekBar) {
           if (mediaPlayer !=null){
               int progress = seekBar.getProgress();
               int total = mediaPlayer.getDuration();
               int position = total*progress/100;
               mediaPlayer.seekTo(position);
           }
           }
       });

       bofan.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               if(mediaPlayer == null) {
                   mediaPlayer = new MediaPlayer();
                   try {
                       mediaPlayer.setDataSource("/mnt/shared/Other/Kalimba.mp3");
                   } catch (IOException e) {
                       e.printStackTrace();
                   }
                   mediaPlayer.prepareAsync();
                   mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                       @Override
                       public void onPrepared(MediaPlayer mediaPlayer) {
                           mediaPlayer.start();
                       }
                   });
               }else{
                   mediaPlayer.start();
               }
           }
       });
       zanting.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               mediaPlayer.pause();
           }
       });

    }
}
